/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.factory;

import Managers.ObjectManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import mygame.model.GameLight;
import mygame.model.GameNode;
import mygame.model.GameTexture;

/**
 *
 * @author xwgou
 */
public class SpatialFactory {

    public static Spatial getSpatial(GameNode node) {
        Spatial ret = null;
        switch (node.getType()) {
            case GEOMETRY_BOX:
                return buildGeometryBox(node);
            case NODE:
                return buildNode(node);
        }

        return ret;
    }

    private static Spatial buildGeometryBox(GameNode node) {
        float[] center = node.getCenter();
        float[] translation = node.getTranslation();
        float[] rotate = node.getRotation();
        Box box = new Box(Vector3f.ZERO, center[0], center[1], center[2]);
        Geometry spatialBox = new Geometry("Box", box);
        String materialName= node.getMaterial();
        
        if(materialName!=null){
                Material material = new Material(
                ObjectManager.getInstance().getAssetManager(),
                node.getMaterial());
                GameTexture textureName= node.getTexture();
                if( textureName != null){
                    material.setTexture(node.getTexture().getType(),
                    ObjectManager.getInstance().getAssetManager().loadTexture(node.getTexture().getPath()));
                }
        spatialBox.setMaterial(material);
        }
        
        if (translation != null) {
            spatialBox.setLocalTranslation(translation[0], translation[1], translation[2]);
        }

        if (rotate != null) {
            spatialBox.rotate(rotate[0], rotate[1], rotate[2]);
        }
        String nodeName = node.getName();
        
        if(nodeName != null){
            spatialBox.setName(nodeName);
        }

        return spatialBox;
    }

    private static Spatial buildNode(GameNode node) {
        Spatial spatial = null;
        spatial = ObjectManager.getInstance().getAssetManager().loadModel(node.getModel());

        Material material;
        if (node.getMaterial() != null) {
            material = new Material(ObjectManager.getInstance().getAssetManager(),
                    node.getMaterial());
            spatial.setMaterial(material);
        }

        float[] translation = node.getTranslation();
        if (translation != null) {
            spatial.setLocalTranslation(translation[0], translation[1], translation[2]);
        }

        float[] rotate = node.getRotation();
        if (rotate != null) {
            spatial.rotate(rotate[0], rotate[1], rotate[2]);
        }

        float[] scale = node.getScale();
        if (scale != null) {
            spatial.scale(scale[0], scale[1], scale[2]);
        }

        if (node.getName() != null) {
            spatial.setName(node.getName());
        }

        return spatial;
    }

    public static Light getLight(GameLight l) {
        switch (l.getType()) {
//            case AMBIENT:
//                return buildAmbientLight(l);
            case DIRECTIONAL:
                return buildDirectionalLight(l);
            case POINT:
                return buildPointLight(l);
        }

        return null;
    }

//    private static Light buildAmbientLight(GameLight l) {
//        DirectionalLight ret = new DirectionalLight();
//        float[] c = l.getColor();
//        ColorRGBA color = new ColorRGBA(c[0], c[1], c[2], c[3]);
//        ret.setColor(color);
//        
//        if (l.getDirection() != null) {
//           
//        ret.setDirection(l.getDistance());
//        }
//        
//        return ret;
//    }

    private static Light buildDirectionalLight(GameLight l) {
        DirectionalLight ret = new DirectionalLight();
        float[] c = l.getColor();
        ColorRGBA color;
        if (c != null) {
            color = new ColorRGBA(c[0], c[1], c[2], c[3]);
            ret.setColor(color);
        }
        
        float[] d = l.getDirection();
        if (d != null) {
            ret.setDirection(new Vector3f(d[0], d[1], d[2]));
        }

        return ret;
    }

    private static Light buildPointLight(GameLight l) {
        PointLight ret = new PointLight();
        float[] c = l.getColor();
        ColorRGBA color;
        if (c != null) {
            color = new ColorRGBA(c[0], c[1], c[2], c[3]);
            ret.setColor(color);
        }
        
        Float r = l.getRadius();
        if (r != null) {
            ret.setRadius(r);
        }
        
        float[] position = l.getPosition();
        if (position != null) {
            ret.setPosition(new Vector3f(position[0], position[1], position[2]));
        }
        
        return ret;
    }
}
